package com.redlion.hud;

import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.HorizontalAlign;
import org.andengine.util.color.Color;

import android.util.Log;

import com.redlion.Game;
import com.redlion.GameActivity;

public class WeaponEntrySprite extends Sprite {
	public static final int DONE = 0;
	public static final int NEXT = 1;
	public static final int DEFAULT = 2;
	Text price;
	Text damage;
	Text delay;
	Text name;
	AnimatedSprite weaponSprite;
	ITiledTextureRegion mWeapons_TTR;
	// private Color COLOR_DONE = new Color(0f, 0.6f, 0.6f, 0.6f);
	private Color COLOR_DEFAULT = new Color(0.8f, 0.6f, 0.6f, 0.5f);
	private Color COLOR_NEXT = new Color(1f, 1f, 1f, 1f);
	private boolean clickable = false;;

	public WeaponEntrySprite(float pX, float pY, float pWidth, float pHeight, ITiledTextureRegion pWeapons_TTR,
			ITextureRegion pCatalogItemBackground_TR, VertexBufferObjectManager vertexBufferObjectManager, Font mFont) {
		super(pX, pY, pWidth, pHeight, pCatalogItemBackground_TR, vertexBufferObjectManager);
		mWeapons_TTR = pWeapons_TTR;
		if (mWeapons_TTR != null) {
			weaponSprite = new AnimatedSprite(10, 20, mWeapons_TTR, Game.gameActivity.getVertexBufferObjectManager());
			this.attachChild(weaponSprite);
		}
		name = new Text(0, 0, mFont, "sssssssssssssssssssssssss", new TextOptions(HorizontalAlign.LEFT),
				Game.gameActivity.getVertexBufferObjectManager());
		price = new Text(0, 0, mFont, "sssssssssssssssssssssssss", new TextOptions(HorizontalAlign.CENTER),
				Game.gameActivity.getVertexBufferObjectManager());
		damage = new Text(0, 0, mFont, "sssssssssssssssssssssssss", new TextOptions(HorizontalAlign.CENTER),
				Game.gameActivity.getVertexBufferObjectManager());
		delay = new Text(0, 0, mFont, "sssssssssssssssssssssssss", new TextOptions(HorizontalAlign.CENTER),
				Game.gameActivity.getVertexBufferObjectManager());
		price.setWidth(100);
		damage.setWidth(50);
		delay.setWidth(50);
		name.setWidth(200);
		price.setX(MyHUD.WEAPONSLOT_WIDTH - 50);
		damage.setX(MyHUD.WEAPONSLOT_WIDTH - 75);
		delay.setX(MyHUD.WEAPONSLOT_WIDTH - 40);
		name.setX(10);
		price.setY(MyHUD.WEAPONSLOT_HEIGHT * 0.3f);
		damage.setY(MyHUD.WEAPONSLOT_HEIGHT * 0.6f);
		delay.setY(MyHUD.WEAPONSLOT_HEIGHT * 0.6f);
		name.setY((MyHUD.WEAPONSLOT_HEIGHT * 0.6f));

		this.attachChild(price);
		this.attachChild(damage);
		this.attachChild(delay);
		this.attachChild(name);

		Game.mScene.registerTouchArea(this);
	}

	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
		if (clickable && pSceneTouchEvent.isActionUp())
			MyHUD.notifyOnCatalogEntryClick(this);
		return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
	}

	public void setName(String n) {
		name.setText(n);
	}

	public void setPrice(int p) {
		price.setText(p + " $");

	}

	public void setDelay(float d) {
		delay.setText(d + "");
	}

	public void setDamage(float d) {
		damage.setText(d + "");
	}

	public void setTileIndex(int pCurrentTileIndex) {
		weaponSprite.setCurrentTileIndex(pCurrentTileIndex);
	}

	public void setState(int state) {
		switch (state) {
		case DONE:
			// this.setColor(COLOR_DONE);
			// alphaChildren(this, COLOR_DONE.getAlpha());
			clickable = false;
			break;
		case DEFAULT:
			this.setColor(COLOR_DEFAULT);
			alphaChildren(this, COLOR_DEFAULT.getAlpha());
			clickable = false;
			break;
		case NEXT:
			this.setColor(COLOR_NEXT);
			alphaChildren(this, COLOR_NEXT.getAlpha());
			clickable = true;
			break;
		default:
			break;
		}
	}

	private void alphaChildren(Sprite s, float pAlpha) {
		for (int i = 0; i < s.getChildCount(); i++) {
			s.getChildByIndex(i).setAlpha(pAlpha);
		}
	}
}
